﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AcademicAudit_JoelShuman
{
    public class StudentRecord
    {
        public StudentRecord( String firstName, String lastName )
		{
			m_StudentID = StudentIDNumbers.GetUniqueID();
			m_FirstName = firstName;
			m_LastName = lastName;
			m_FileName = m_StudentID + "_" + m_LastName + "_" + m_FirstName;
			m_FullName = m_FirstName + " " + m_LastName;
			m_CourseList = DefaultCourseList.DEFAULTCOURSELIST;
			m_CoreCompletionPercentage = CalculateCourseCompletionPercentage( CourseType.CORE );
			m_GenEdCompletionPercentage = CalculateCourseCompletionPercentage(CourseType.GENED);
			m_GPA = CalculateGPA();
        }
		public StudentRecord(String firstName, String lastName, String studentID)
		{
			m_StudentID = studentID;
			m_FirstName = firstName;
			m_LastName = lastName;
			m_FullName = m_FirstName + " " + m_LastName;
			m_CourseList = DefaultCourseList.DEFAULTCOURSELIST;
			m_CoreCompletionPercentage = CalculateCourseCompletionPercentage(CourseType.CORE);
			m_GenEdCompletionPercentage = CalculateCourseCompletionPercentage(CourseType.GENED);
			m_FileName = m_StudentID + "_" + m_LastName + "_" + m_FirstName;
			m_GPA = CalculateGPA();
		}

		public StudentRecord(){}

		public String m_StudentID { get; set; }
		public String m_FirstName { get; set; }
		public String m_LastName { get; set; }
		public String m_FullName { get; set; }
		public String m_FileName { get; set; }
		public double m_CoreCompletionPercentage { get; set; }
		public double m_GenEdCompletionPercentage { get; set; }

		public double m_GPA { get; set; }

		public List<Course> m_CourseList{ get; set; }

		public double CalculateCourseCompletionPercentage( CourseType type )
		{
			int totalClasses = m_CourseList.Count;
			int completedClasses = 0;

			foreach( Course theCourse in m_CourseList )
			{
				// If it is the type of course we are interested in and it is complete...
				if( theCourse.m_CourseType == type && theCourse.m_CourseStatus == Status.C )
				{
					completedClasses++;
				}
			}

			return Math.Round( ((double)completedClasses/(double)totalClasses)*100, 2 );
		}

		public double CalculateGPA()
		{
			//Gpa = (Sum for each class of letter grade point * credit hours)/sum of credit hours
			double theGPA = 0.0;
			int totalCreditHours = 0;
			double sumGradexCredits = 0.0;

			// Get the students letter grade and credit hours.
			foreach( Course thisCourse in m_CourseList )
			{
				if( thisCourse.m_CourseGrade != Grade.UNKNOWN )
				{
					totalCreditHours += thisCourse.m_Credits;
					sumGradexCredits += GetGradeValue(thisCourse.m_CourseGrade) * thisCourse.m_Credits;
				}
			}

			if( totalCreditHours > 0 )
				theGPA = sumGradexCredits / (double)totalCreditHours;

			return Math.Round( theGPA, 2 );
		}

		private int GetGradeValue( Grade theGrade )
		{
			int returnVal = 0;
			switch( theGrade )
			{
				case Grade.A:
					returnVal = 4;
					break;
				case Grade.B:
					returnVal = 3;
					break;
				case Grade.C:
					returnVal = 2;
					break;
				case Grade.D:
					returnVal = 1;
					break;
				case Grade.F:
					returnVal = 0;
					break;
				default:
					returnVal = 0;
					break;
			}
			return returnVal;
		}
    }
}
